package com.zt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zt.service.BannerService;

import com.zt.uitl.Query;
import com.zt.uitl.Result;
import org.example.systemset_pojo.Banner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@RestController
@RequestMapping("/banner")
public class BannerController {
    @Autowired
    private BannerService bannerService;

    @RequestMapping("/selectpage")
    public Result selectpage(Query query){
        Page<Banner> page = bannerService.selectpage(query);
        return Result.OK(page);
    }

    @RequestMapping("/update")
    public Result update(Banner banner){
        boolean b = bannerService.updateById(banner);
        if (b){
            return Result.OK();
        }
        return Result.ERROR();
    }

    @RequestMapping("/updateStatus")
    public Result updateStatus(Integer id,Integer status){
        Banner banner = new Banner();
        Banner byId = bannerService.getById(id);

        banner.setBannerId(byId.getBannerId());
        banner.setBannerName(byId.getBannerName());
        banner.setSort(byId.getSort());
        banner.setJumpUrl(byId.getJumpUrl());
        banner.setImageUrl(byId.getImageUrl());
        banner.setIsActive(status);
        banner.setStoreId(byId.getStoreId());
        boolean b = bannerService.updateById(banner);
        if (b){
            return Result.OK();
        }
        return Result.ERROR();
    }

    @RequestMapping("/updateSort")
    public Result updateSort(Integer id,Integer sort){
        Banner banner = new Banner();
        Banner byId = bannerService.getById(id);

        banner.setBannerId(id);
        banner.setBannerName(byId.getBannerName());
        banner.setSort(sort);
        banner.setJumpUrl(byId.getJumpUrl());
        banner.setImageUrl(byId.getImageUrl());
        banner.setIsActive(byId.getIsActive());
        banner.setStoreId(byId.getStoreId());
        boolean b = bannerService.updateById(banner);
        if (b){
            return Result.OK();
        }
        return Result.ERROR();
    }

    @RequestMapping("/del")
    public Result del(Integer id){
        boolean b = bannerService.removeById(id);
        if (b){
            return Result.OK();
        }
        return Result.ERROR();
    }

//    批量删除
    @RequestMapping("/batchDelete")
    public Result batchDelete(@RequestBody Integer[] ids){
        boolean b = bannerService.removeByIds(Arrays.asList(ids));
        if (b){
            return Result.OK();
        }
        return Result.ERROR();
    }

//    添加banner图
    @RequestMapping("/add")
    public Result add(Banner banner){
        boolean save = bannerService.save(banner);
        if (save){
            return Result.OK();
        }
        return Result.ERROR();
    }
}
